import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.LinkedList;
import java.util.Scanner;

class MDHandler extends Thread 
{ 
	private Socket clientSock;
    private MDServer chatServer;
    MDHandler(Socket s, MDServer cs) 
    {
        clientSock = s;   chatServer = cs;
    }
    
    public void sendShapeObjects(){
    	
    	ObjectOutputStream oos;
		try {
			oos = new ObjectOutputStream(clientSock.getOutputStream());
			oos.writeObject(chatServer.ShapeObjects);
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    public void run() 
    {
    	sendShapeObjects();
    	
        try 
        {   
        	
        	Scanner in =
              new Scanner(clientSock.getInputStream());
            PrintWriter out = 
              new PrintWriter(new OutputStreamWriter(clientSock.getOutputStream()),
                                                     true);
    	    		
            out.println("<<USRMSG>>>>>> Welcome to the MD Server <<<<\n");
            out.println("<<USRMSG>>Type \'bye\' to exit MD system.\n");

            out.println("<<USRMSG>>What is your name?"); 
            String name = in.nextLine().substring(10);
            chatServer.broadcast("<<USRMSG>>" + name + " has joined discussion.", "Chat Server");
     
            while (true) 	
            {
            	String message = in.nextLine().trim();
            	if (message.equals("bye")) 
                {
            	    chatServer.broadcast(name + " has left discussion.", "Chat Server");
            	    break;
                }else if(message.contains("<<REF>>")){
            		chatServer.broadcast("<<REFSHAPEOBJS>>", name);
            		chatServer.broadcastShapeObjects();
                }else if(message.contains("<<UPDATESHAPES>>")){
                	PrintWriter p = new PrintWriter(clientSock.getOutputStream(), true);
            	    p.println("<<READY>>");	
                	getShapeObjects();
                	p.println("<<GOTEM>>");
                }else if(message.contains("USRMSG")){
                	chatServer.broadcast(message,name);
                }
            	System.out.println(message);
            }
            chatServer.remove(clientSock);
            clientSock.close();
        }
        catch (IOException e) { System.out.println("Chat error: " + e); }
    }

    public boolean getShapeObjects(){
		   try {
			   ObjectInputStream ois = new ObjectInputStream(clientSock.getInputStream());
			   chatServer.refreshShapeObjects((LinkedList<ShapeObject>)ois.readObject());
		   } catch (IOException e) {
			   e.printStackTrace();
			   return false;
		   } catch (ClassNotFoundException e) {
			   e.printStackTrace();
			   return false;
		   }catch (Exception e){
			   e.printStackTrace();
			   return false;
		   }
		   return true;
	   }
}